/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 5/16/2011
*/

@isTest

private class CaseBusinessDayTester {
	
	static testMethod void testBusinessDayCalculatin(){
		DateTime openDateTime, closeDateTime;
		Decimal BusinessDay;
		Decimal BusinessHour;
		openDateTime  = Datetime.valueOf('2011-05-16 21:01:00'); 
		closeDateTime = Datetime.valueOf('2011-05-16 21:59:00');  
		BusinessDay = CaseBusinessDayHelper.calculateBusinessDayAge(openDateTime, closeDateTime, true, null);
		BusinessHour = CaseBusinessDayHelper.calculateBusinessHourAge(openDateTime, closeDateTime, true, null);
		System.assertEquals(0, BusinessDay);
		System.assertEquals(0.00, BusinessHour);
		
		openDateTime  = Datetime.valueOf('2011-05-13 10:01:00'); 
		closeDateTime = Datetime.valueOf('2011-05-13 11:11:00');  
		BusinessDay = CaseBusinessDayHelper.calculateBusinessDayAge(openDateTime, closeDateTime, true, null);
		BusinessHour = CaseBusinessDayHelper.calculateBusinessHourAge(openDateTime, closeDateTime, true, null);
		System.assertEquals(1, BusinessDay);
		System.assertEquals(1.17, BusinessHour);	

		openDateTime  = Datetime.valueOf('2011-05-14 0:01:00'); 
		closeDateTime = Datetime.valueOf('2011-05-15 21:29:00');  
		BusinessDay = CaseBusinessDayHelper.calculateBusinessDayAge(openDateTime, closeDateTime, true, null);
		BusinessHour = CaseBusinessDayHelper.calculateBusinessHourAge(openDateTime, closeDateTime, true, null);
		System.assertEquals(0, BusinessDay);
		System.assertEquals(0.00, BusinessHour);

		openDateTime  = Datetime.valueOf('2011-05-13 13:01:00'); 
		closeDateTime = Datetime.valueOf('2011-05-16 13:11:01');  
		BusinessDay = CaseBusinessDayHelper.calculateBusinessDayAge(openDateTime, closeDateTime, true, null);
		BusinessHour = CaseBusinessDayHelper.calculateBusinessHourAge(openDateTime, closeDateTime, true, null);
		System.assertEquals(2, BusinessDay);
		System.assertEquals(13.17, BusinessHour);

		openDateTime  = Datetime.valueOf('2011-05-16 13:01:00'); 
		closeDateTime = Datetime.valueOf('2011-05-16 13:01:01');  
		BusinessDay = CaseBusinessDayHelper.calculateBusinessDayAge(openDateTime, closeDateTime, true, null);
		BusinessHour = CaseBusinessDayHelper.calculateBusinessHourAge(openDateTime, closeDateTime, true, null);
		System.assertEquals(1, BusinessDay);
		System.assertEquals(0.01, BusinessHour);
				
		openDateTime  = Datetime.valueOf('2011-07-04 0:01:00'); 
		closeDateTime = Datetime.valueOf('2011-07-04 21:29:00');  
		BusinessDay = CaseBusinessDayHelper.calculateBusinessDayAge(openDateTime, closeDateTime, true, null);
		BusinessHour = CaseBusinessDayHelper.calculateBusinessHourAge(openDateTime, closeDateTime, true, null);
		System.assertEquals(0, BusinessDay);
		System.assertEquals(0.00, BusinessHour);
	}
	
	static testmethod void testCaseScheduler(){
		Test.startTest();
		CaseBusinessDayAgeScheduler a0 = new CaseBusinessDayAgeScheduler(false);
		String sch0 = CaseBusinessDayAgeScheduler.CRON_EXP_WEST_User_Schedule;
		ID jobId = System.schedule('Test Schedule Batch upate case Job0', sch0, a0);
		Test.stopTest();
		CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
		System.assertEquals(CaseBusinessDayAgeScheduler.CRON_EXP_WEST_User_Schedule, ct.CronExpression); 
		System.assertEquals(0, ct.TimesTriggered);
	}
	
	static testmethod void testBatchUpdateCase(){
		CaseBusinessDayAgeScheduler b = new CaseBusinessDayAgeScheduler(false);
		ID batchUpdateCaseId = Database.executeBatch(b);
		AsyncApexJob ajob = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID =: batchUpdateCaseId ];
		System.AssertEquals(0, ajob.NumberOfErrors);		
	}
	
	static testmethod void testCaseCycles(){
	    User u_new = new User();
		u_new.UserName = 'xyz_1234567890@atti-temp.com' + 'x';
		u_new.LastName = 'Michael Shu Test Email' + 'x';
		u_new.Email = 'xyz_1234567890@atti.com';
		u_new.Alias = 'abcde' + 'x';
		u_new.CommunityNickname = 'xyaz_1234556' + 'x';
		u_new.TimeZoneSidKey = 'America/Los_Angeles';
		u_new.LocaleSidKey = 'en_US';
		u_new.EmailEncodingKey = 'UTF-8';
		u_new.Cycle_Team__c = 'IOG 1';
		u_new.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
		u_new.UserRoleId = [select ID from UserRole where Name = 'Information Systems' limit 1].Id;
		u_new.LanguageLocaleKey = 'en_US';
		Database.Saveresult sr = Database.insert(u_new);
		
		User sysUser = [select ID from User where ID =: sr.getID() limit 1];

	    User u_new2 = new User();
		u_new2.UserName = 'xyz_1234567890@atti-temp.com' + 'y';
		u_new2.LastName = 'Michael Shu Test Email' + 'y';
		u_new2.Email = 'xyz_1234567890@atti.com';
		u_new2.Alias = 'abcde' + 'y';
		u_new2.CommunityNickname = 'xyaz_1234556' + 'y';
		u_new2.TimeZoneSidKey = 'America/Los_Angeles';
		u_new2.LocaleSidKey = 'en_US';
		u_new2.EmailEncodingKey = 'UTF-8';
		u_new2.Cycle_Team__c = 'IOG 2';
		u_new2.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
		u_new2.UserRoleId = [select ID from UserRole where Name = 'Information Systems' limit 1].Id;
		u_new2.LanguageLocaleKey = 'en_US';
		Database.Saveresult sr2 = Database.insert(u_new2);
		
		User sysUser2 = [select ID from User where ID =: sr2.getID() limit 1];
				
		Case c = new Case();
		c.Subject = 'testCaseCycles';
		c.Origin = 'Phone';
		c.Status = 'Open';
		c.IR_Priority__c = 'Standard';
		c.CSS_CRF_RequestType__c = 'Change Task';
		c.CSS_ProductFamily__c = 'ATT411';
		c.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case', 'IOG Incident');
		Database.Saveresult s_rst;
		system.runAs(sysUser){ 
			s_rst = Database.insert(c);
		}
		Case c_rst = [select id from Case where id =: s_rst.getID() limit 1];
		list<Case_Cycle__c> cclist = [select Change_Type__c, Start_Date__c, End_Date__c, Value__c from Case_Cycle__c where ParentCase__c =: s_rst.getID()];
		system.assertEquals(4, cclist.size());
		for(Case_Cycle__c cc : cclist){
			if(cc.Change_Type__c == 'Owner'){
				system.assertEquals('IOG 1', cc.Value__c);
			}
			if(cc.Change_Type__c == 'Status'){
				system.assertEquals('Open', cc.Value__c);
			}
			if(cc.Change_Type__c == 'IR Priority'){
				system.assertEquals('Standard', cc.Value__c);
			}
		}
		c_rst.OwnerId = sysUser2.Id;
		Database.Saveresult s_rst2;
		system.runAs(sysUser){ 
			s_rst2 = Database.update(c_rst);
		}		
		list<Case_Cycle__c> cclist2 = [select Change_Type__c, Start_Date__c, End_Date__c, Value__c from Case_Cycle__c where ParentCase__c =: s_rst.getID()];
		system.assertEquals(5, cclist2.size());
		for(Case_Cycle__c cc : cclist2){
			if(cc.Change_Type__c == 'Owner' && cc.End_Date__c != null){
				system.assertEquals('IOG 1', cc.Value__c);
			}
			if(cc.Change_Type__c == 'Owner' && cc.End_Date__c == null){
				system.assertEquals('IOG 2', cc.Value__c);
			}	
		}
	}
}